package lt.egidijus.aatest.popup.actions;

import java.util.ArrayList;
import java.util.List;

import lt.egidijus.aatest.runner.AATestJunitLauncher;
import lt.egidijus.aatest.utils.Constants;
import lt.egidijus.aatest.utils.Utils;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.android.ide.eclipse.adt.internal.launch.junit.AndroidJUnitLaunchShortcut;

public class JunitTestRunnerArction  implements IObjectActionDelegate {

	/**
	 * Constructor for Action1.
	 */
	public JunitTestRunnerArction() {
		super();
	}

	private Shell shell;

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}
	ISelection lastSelection = null;

	List<ICompilationUnit> list = new ArrayList<ICompilationUnit>(0);

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
	//	AATestJunitLauncher laucher = new AATestJunitLauncher();
	//	laucher.launch(lastSelection, "run");
		if (!Constants.clickTestProject){
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					MessageDialog.openError(shell, "Run error", "Run can only test in test project!");
				}
			});
			return;
		}
		AndroidJUnitLaunchShortcut launch = new AndroidJUnitLaunchShortcut();
		launch.launch(lastSelection, "run");

	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		Utils.updateProjectsNames(selection);
		if (selection instanceof IStructuredSelection)
				this.lastSelection = selection;

	}
}